package com.objectdraw.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import com.objectdraw.server.commands.Command;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * The purpose of this class is to take a request from the client, wrap it in an object
 * the request processors can understand and then pass that reqest to the correct processor
 * @author Harrison
 */
public class ObjectDrawHandler extends Thread {

	private Socket client = null;

	private CommunicationServer comm = null;

	public ObjectDrawHandler(Socket client, CommunicationServer server) {
		this.client = client;
		comm = server;
	}

	public void run() {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));
			while (true) {
				if(!client.isClosed()){
				String requestString = in.readLine();
				RequestData req = new RequestData();
				req.setClient(client);
				req.setCommServer(comm);
				req.setCommand(requestString.substring(0, requestString
						.indexOf(" ")));
				req.setParams(requestString.substring(requestString
						.indexOf(" "), requestString.length()));
				
				Command cmd = ObjectDrawServer.getCommandHandlers();
				cmd.processRequest(req, new ResponseData());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	

}
